//
//  TKYSettingController.m
//  StationHouseHelper
//
//  Created by apple on 2018/6/14.
//  Copyright © 2018年 铁科院. All rights reserved.
//

#import "TKYSettingController.h"
#import "TKYTabBarController.h"
#import "TKYSettingCell.h"
#import "TKYCleanCaches.h"
#import "TKYChangePwdController.h"
#import "TKYFeedbackController.h"
#import "TKYProgressDateController.h"
#define kSettingCellID @"TKYSettingCell"
@interface TKYSettingController ()<UITableViewDelegate,UITableViewDataSource>
{
    CGFloat _size;
}

@property (nonatomic, strong)NSString *cachesDir;
@property (nonatomic, assign)BOOL  isCaches;
@property (nonatomic, strong)BaseTabelView *tabelView;


@end

@implementation TKYSettingController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 设置界面
    [self setupUI];
}


//设置界面
- (void)setupUI{
    self.navigationItem.title = @"设置";
    self.navigationController.navigationBar.barTintColor = KNavColor;
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:19.0]}];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"back_btn"] style:UIBarButtonItemStylePlain target:self action:@selector(backBarButtonItemAction:)];
    
    [self.view addSubview:self.tabelView];
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    self.cachesDir= [paths objectAtIndex:0];
    _size = [TKYCleanCaches folderSizeAtPath:_cachesDir];
}

//返回按钮的点击事件
- (void)backBarButtonItemAction:(UIBarButtonItem *)barButtonItem{
    UIViewController * presentingViewController = self.presentingViewController;
    do {
        if ([presentingViewController isKindOfClass:[TKYTabBarController class]]) {
            break;
        }
        presentingViewController = presentingViewController.presentingViewController;
        
    } while (presentingViewController.presentingViewController);
    
    [presentingViewController dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark---UITableViewDelegate,UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    TKYSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:kSettingCellID];
    switch (indexPath.row) {
        case 0:
        {
            cell.titleLabel.text = @"修改密码";
            cell.contentLabel.text = @"";
            cell.isTouch = YES;
        }
            break;
        case 1:
        {
            cell.titleLabel.text = @"进度提醒设置";
            cell.contentLabel.text = @"";
            cell.isTouch = YES;
        }
            break;
        case 2:
        {
            NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
            NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
            cell.titleLabel.text = @"当前版本";
            cell.contentLabel.text = app_Version;
            cell.isTouch = NO;
        }
            break;
        case 3:
        {
            cell.titleLabel.text = @"清楚缓存";
            NSString *message = _size > 1 ? [NSString stringWithFormat:@"%.2fM", _size] : [NSString stringWithFormat:@"%.2fK", _size * 1024.0];
            cell.contentLabel.text = message;
            cell.isTouch = YES;
        }
            break;
        case 4:
        {
            cell.titleLabel.text = @"意见反馈";
            cell.contentLabel.text = @"";
            cell.isTouch = YES;
        }
            break;
            
        default:
            break;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        TKYChangePwdController *changePwdVC = [[TKYChangePwdController alloc]init];
        NSLog(@"%@",changePwdVC.view);
        [self.navigationController pushViewController:changePwdVC animated:YES];
        
    }else if (indexPath.row == 1){
        TKYProgressDateController *dateVC = [[TKYProgressDateController alloc]init];
        [self.navigationController pushViewController:dateVC animated:YES];
    }else if (indexPath.row == 2){
      // KShowAlertWithMsg_ios8(@"当前最新版本");
    }else if (indexPath.row == 3){
        [self cleanCaaches];
    }else{
        TKYFeedbackController *feedbackVc = [[TKYFeedbackController alloc]init];
        [self.navigationController pushViewController:feedbackVc animated:YES];

    }
}

#pragma mark-- 清理缓存
- (void)cleanCaaches{
    self.isCaches = YES;
    // _size = [TKYCleanCaches folderSizeAtPath:_cachesDir];
    [SVProgressHUD showSVProgressImage:nil Duration:2.0 FontSize:16 status:@"正在清理"];
    [TKYCleanCaches clearCache:self.cachesDir];
    
    _size = 0;
    [self.tabelView reloadData];
    [SVProgressHUD showSVProgressImage:nil Duration:2.0 FontSize:16 status:@"清理完毕"];
}

#pragma mark-- lazy
- (BaseTabelView *)tabelView{
    if (_tabelView == nil) {
        _tabelView = [[BaseTabelView alloc]initWithFrame:CGRectMake(0, MyNavBarHeight, kScreenWidth, kScreenHeight) style:UITableViewStylePlain];
        _tabelView.dataSource = self;
        _tabelView.delegate = self;
        [_tabelView registerClass:[TKYSettingCell class] forCellReuseIdentifier:kSettingCellID];
        _tabelView.tableFooterView = [[UIView alloc]init];
    }
    return _tabelView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
